package com.example.zhuyu.basemvp.utils;

import com.example.zhuyu.basemvp.base.IView;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.trello.rxlifecycle2.components.support.RxFragment;

/**
 * @desc: Rx生命周期管理
 * @author: hexl
 * @date: 2018/7/28
 */
public class RxLifecycleUtil {

    /**
     * 解除订阅关系
     *
     * @param view 框架采用mvp 设计模式,该{@link IView}
     *             开发人员定义Contract类时应该去继承.所以该view是前台页面展示的Activity or Fragment
     * @return ObservableTransformer
     */
    public static <T> LifecycleTransformer<T> bindUntilEvent(IView view) {
        if (view instanceof RxAppCompatActivity) {
            return ((RxAppCompatActivity) view).bindUntilEvent(ActivityEvent.DESTROY);
        }
        if (view instanceof RxFragment) {
            return ((RxFragment) view).bindUntilEvent(FragmentEvent.DESTROY);
        }
        return null;
    }
}
